package com.dream.wuliu.base;

import java.net.SocketTimeoutException;

import retrofit.ErrorHandler;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * 网络请求错误处理
 * @author EvanYu
 *
 */
public class MyErrorHandler implements ErrorHandler {
	
	@Override
	public Throwable handleError(RetrofitError cause) {
		
			//网络异常
			if(cause.isNetworkError()){
				 if (cause.getCause() instanceof SocketTimeoutException) {
					
					 return new RequestTimeoutException();
		         } 
				 return new NetworkException();
			}
			
		    Response r = cause.getResponse();
		    //系统异常
		    if (r != null) {
		    	int status = r.getStatus();
		    	
		    	if(status == StatusCode.EX_SYS){
		    		 return new SysException();
		    	}
		    	
		    	if(status == StatusCode.NOT_AUTHORIZE){
		    		 return new AuthorizeException();
		    	}
		    	
		    }
		    return cause;
	}
	
	public static class NetworkException extends RuntimeException{
		private static final long serialVersionUID = 857030269437838538L;
	}
	public static class RequestTimeoutException extends RuntimeException{
		private static final long serialVersionUID = -6108746755800959402L;
	}
	public static class SysException extends RuntimeException{
		private static final long serialVersionUID = 4210861481830041638L;
	}
	public static class AuthorizeException extends RuntimeException{
		
		private static final long serialVersionUID = -7871884329646087525L;
	}
	
}
